package com.briup.cms.service.impl;

import com.briup.cms.bean.Category;
import com.briup.cms.dao.CategoryExtendMapper;
import com.briup.cms.dao.CategoryMapper;

 import com.briup.cms.exception.CustomerException;
 import com.briup.cms.service.ICategoryService;
import com.briup.cms.utils.MessageStatus;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import java.util.ArrayList;
 import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;

 @Service
 public class CategoryServiceImpl
         implements ICategoryService
        {
     @Autowired
     private CategoryMapper categoryMapper;
     @Autowired
      private CategoryExtendMapper categoryExtendMapper;

   public PageInfo<Category> pageQueryAll(int pageNum, int pageSize) {
           PageHelper.startPage(pageNum, pageSize, true);

         List<Category> list = this.categoryExtendMapper.pageQueryAll();

          if (list == null) {
                list = new ArrayList<>();
             }

            return new PageInfo(list);
         }

       @Transactional
     public void deleteById(int id) throws CustomerException {
           Category category = this.categoryMapper.selectByPrimaryKey(Integer.valueOf(id));
            if (category == null) {
                   throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CATEGORY_NOT_EXIST);
                }

             this.categoryMapper.deleteByPrimaryKey(Integer.valueOf(id));
        }


    @Transactional
   public void batchDelete(int[] ids) throws CustomerException {
           for (int id : ids) {
                 deleteById(id);
              }
         }


       @Transactional
    public void saveCategory(Category category) throws CustomerException {
           this.categoryMapper.insert(category);   }

     @Transactional
      public void updateCategory(Category category) throws CustomerException {
            this.categoryMapper.updateByPrimaryKeySelective(category);
         }
        @Override
      public List<Category> findAll() throws CustomerException {
            List<Category> list = this.categoryExtendMapper.findAll();
           if (list == null) {
                 list = new ArrayList<>();
               }
          return list;
       }
  }
